package com.pn.service.impl;

import com.pn.dto.AssignRoleDto;
import com.pn.entity.Result;
import com.pn.entity.User;
import com.pn.entity.UserRole;
import com.pn.mapper.RoleMapper;
import com.pn.mapper.UserMapper;
import com.pn.mapper.UserRoleMapper;
import com.pn.page.Page;
import com.pn.service.UserService;
import com.pn.utils.DigestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author :LY
 * @Description :
 * @Date :2025/3/18 20:46
 */

@Service
public class UserserviceImpl implements UserService {

    //注入UserMapper
    @Autowired
    private UserMapper userMapper;

    //根据账号查询用户的业务方法
    @Override
    public User queryUserByCode(String userCode) {
        return userMapper.findUserByCode(userCode);
    }

    @Override
    public Page queryUserByPage(Page page, User user) {
        //查询用户行数
        Integer count = userMapper.findUserRowCount(user);

        //分页查询用户
        List<User> userList = userMapper.findUserByPage(page,user);

        //组装所有分页信息
        page.setTotalNum(count);
        page.setResultList(userList);
        return page;
    }

    @Override
    public Result saveUser(User user) {
        //判断账号是否已存在
        User u = userMapper.findUserByCode(user.getUserCode());
        if(u!=null){
            return Result.err(Result.CODE_ERR_BUSINESS,"账号已存在！");
        }

        //对密码做加密处理
        String password = DigestUtil.hmacSign(user.getUserPwd());
        user.setUserPwd(password);

        //执行添加
        int i= userMapper.insertUser(user);
        if(i>0){
            return Result.ok("用户添加成功！");
        }
        return Result.err(Result.CODE_ERR_BUSINESS,"用户添加失败！");
    }

    //启用或禁用用户的业务方法
    @Override
    public Result setUserState(User user) {
        int i = userMapper.setStateByUid(user.getUserId(),user.getUserState());
        if(i>0){
            return Result.ok("启用或禁用用户成功！");
        }
        return Result.err(Result.CODE_ERR_BUSINESS,"启用或禁用用户失败！");
    }

    /*
        添加用户分配角色关系的方法
     */
    //注入RoleMapper
    @Autowired
    private RoleMapper roleMapper;

    //注入UserRoleMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;

    @Transactional//事务处理
    @Override
    public void assignRole(AssignRoleDto assignRoleDto) {

        userRoleMapper.removeUserRoleByUid(assignRoleDto.getUserId());

            List<String> roleNameList = assignRoleDto.getRoleCheckList();
            for (String roleName : roleNameList){
                Integer roleId = roleMapper.findRoleIdByName(roleName);
                UserRole userRole = new UserRole();
                userRole.setRoleId(roleId);
                userRole.setUserId(assignRoleDto.getUserId());
                userRoleMapper.insertUserRole(userRole);
            }
    }

    @Override
    public Result removeUserByIds(List<Integer> userIdList) {
        int i = userMapper.setIsDeleteByUids(userIdList);
        if (i>0){
            return Result.ok("用户删除成功！");
        }
        return Result.err(Result.CODE_ERR_BUSINESS,"用户删除失败！");
    }

    @Override
    public Result setUserById(User user) {
        int i = userMapper.setUserNameByUid(user);
        if (i>0){
            return Result.ok("用户修改成功！");
        }
        return Result.err(Result.CODE_ERR_BUSINESS,"用户修改失败！");
    }

    @Override
    public Result setPwdById(Integer userId) {

        //给定初始密码123456并加密
        String password = DigestUtil.hmacSign("123456");

        //根据用户id修改密码
        int i = userMapper.setPwdByUid(userId,password);
        if (i>0){
            return Result.ok("密码重置成功！");
        }
        return Result.err(Result.CODE_ERR_BUSINESS,"密码重置失败！");
    }


}